<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;


    public function showRegistrationForm()
    {
        return view('auth.register');
    }


    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $data1 = [
            'name'      =>  $data['txtUserName'],
            'username'  =>  $data['username'],
            'password'  =>  $data['txtPassword'],
            'password_confirmation'  =>  $data['txtPassword1'],
            'phone'     =>  $data['txtMobile'],
            'chkAgree'  =>  $data['chkAgree'],
        ];

        return Validator::make($data1, [
            'name'      => ['required', 'string', 'max:25'],
            'username'  => ['required', 'string'],
            'password'  => ['required', 'string', 'min:6', 'confirmed'],
            'phone'     => ['required', 'string', 'regex:/^1[34578][0-9]{9}$/'],
            'chkAgree'  => ['required'],
        ], [
            'username.required' => '用户名不可以空',
            'name.required'     => '登陆帐号不可以空',
            'name.max'          => '登陆帐号长度25',
            'password.required' => '密码不可为空',
            'password.min'      => '密码最少6位',
            'password.confirmed'=> '两次密码不一致',
            'phone.required'    => '手机号不可为空',
            'phone.regex'       => '手机号格式不对',
            'chkAgree.required' => '注册许可协议必选',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        $data1 = [
            'name'      =>  $data['txtUserName'],
            'username'  =>  $data['username'],
            'password'  =>  $data['txtPassword'],
            'phone'     =>  $data['txtMobile'],
        ];

        return User::create([
            'name'      => $data1['name'],
            'username'  => $data1['username'],
            'phone'     => $data1['phone'],
            'email'     => $data1['phone'].'@qq.com',
            'password' => Hash::make($data1['password']),
        ]);
    }
}
